package com.pkdl;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.atomic.AtomicReference;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

import com.pkdl.enums.LoaiNguoiDung;
import com.pkdl.forms.DlgBenhNhanHienThoi;
import com.pkdl.forms.DlgCapNhatBenhNhanHienThoi;
import com.pkdl.forms.DlgQuanLyBenhNhan;
import com.pkdl.forms.DlgQuanLyDichVu;
import com.pkdl.forms.DlgQuanLyNguoiDung;
import com.pkdl.forms.DlgQuanLyPhieuKham;
import com.pkdl.forms.DlgQuanLyThuoc;
import com.pkdl.forms.DlgTiepNhanBenhNhan;
import com.pkdl.helpers.CommonVars;

public class MainApp {

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainApp window = new MainApp();
					window.frmMain.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private JFrame frmMain;
	private JButton btnCapNhatBenhNhanHienThoi;
	private JButton btnTiepNhanBenhNhan;
	private JButton btnBenhNhanHienThoi;

	/**
	 * Create the application.
	 */
	public MainApp() {
		this.initialize();
		this.showLoginForm();
		this.enableAndDisableBtns();
	}

	private void enableAndDisableBtns() {
		// Enable/Disable btnCapNhatBenhNhanHienThoi
		if (CommonVars.g_oNguoiDung.getLoaiNguoiDung().equals(
				LoaiNguoiDung.LETAN.toString())) {
			this.btnCapNhatBenhNhanHienThoi.setEnabled(true);
			this.btnTiepNhanBenhNhan.setEnabled(false);
			this.btnBenhNhanHienThoi.setEnabled(false);
		} else {
			this.btnCapNhatBenhNhanHienThoi.setEnabled(false);
			this.btnTiepNhanBenhNhan.setEnabled(true);
			this.btnBenhNhanHienThoi.setEnabled(true);
		}

	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		this.frmMain = new JFrame();
		this.frmMain.setTitle("Quan ly phong kham da lieu");
		this.frmMain.setBounds(100, 100, 807, 542);
		this.frmMain.setLocationRelativeTo(null);
		this.frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JMenuBar menuBar = new JMenuBar();
		this.frmMain.setJMenuBar(menuBar);

		JMenu mnHe = new JMenu("H\u1EC7 th\u1ED1ng");
		menuBar.add(mnHe);

		JMenuItem menDangXuat = new JMenuItem("\u0110\u0103ng xu\u1EA5t");
		menDangXuat.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CommonVars.g_oNguoiDung = null;
				MainApp.this.frmMain.setVisible(false);
				MainApp.this.showLoginForm();
				MainApp.this.frmMain.setVisible(true);
			}
		});
		menDangXuat.setIcon(new ImageIcon(MainApp.class
				.getResource("/com/pkdl/resources/logout.png")));
		mnHe.add(menDangXuat);

		JMenuItem menDoiMatKhau = new JMenuItem(
				"\u0110\u1ED5i m\u1EADt kh\u1EA9u");
		menDoiMatKhau.setIcon(new ImageIcon(MainApp.class
				.getResource("/com/pkdl/resources/refresh.png")));
		mnHe.add(menDoiMatKhau);

		JSeparator separator = new JSeparator();
		mnHe.add(separator);

		JMenuItem menQuanLyNguoiDung = new JMenuItem(
				"Qu\u1EA3n l\u00FD ng\u01B0\u1EDDi d\u00F9ng");
		menQuanLyNguoiDung.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				DlgQuanLyNguoiDung dlg = new DlgQuanLyNguoiDung(null, true);
				dlg.show();
			}
		});
		menQuanLyNguoiDung.setIcon(new ImageIcon(MainApp.class
				.getResource("/com/pkdl/resources/User.png")));
		mnHe.add(menQuanLyNguoiDung);

		JMenuItem menThoat = new JMenuItem("Tho\u00E1t");
		menThoat.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		mnHe.add(menThoat);

		JMenu mnNghiepV = new JMenu("Nghi\u1EC7p v\u1EE5");
		menuBar.add(mnNghiepV);

		JMenuItem menQuanLyThuoc = new JMenuItem("Qu\u1EA3n l\u00FD thu\u1ED1c");
		menQuanLyThuoc.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DlgQuanLyThuoc dlg = new DlgQuanLyThuoc(null, true);
				dlg.show();
			}
		});
		mnNghiepV.add(menQuanLyThuoc);

		JMenuItem menQuanLyDichVu = new JMenuItem(
				"Qu\u1EA3n l\u00FD d\u1ECBch v\u1EE5");
		menQuanLyDichVu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DlgQuanLyDichVu dlg = new DlgQuanLyDichVu(null, true);
				dlg.show();
			}
		});
		mnNghiepV.add(menQuanLyDichVu);

		JMenuItem menQuanLyBenhNhan = new JMenuItem(
				"Qu\u1EA3n l\u00FD b\u1EC7nh nh\u00E2n");
		menQuanLyBenhNhan.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DlgQuanLyBenhNhan dlg = new DlgQuanLyBenhNhan(null, true);
				dlg.show();
			}
		});
		mnNghiepV.add(menQuanLyBenhNhan);

		JMenuItem menQuanLyPhieuKham = new JMenuItem(
				"Qu\u1EA3n l\u00FD phi\u1EBFu kh\u00E1m");
		menQuanLyPhieuKham.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DlgQuanLyPhieuKham dlg = new DlgQuanLyPhieuKham(null, true);
				dlg.show();
			}
		});
		mnNghiepV.add(menQuanLyPhieuKham);

		JPanel panel = new JPanel();
		this.frmMain.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));

		JToolBar toolBar = new JToolBar();
		panel.add(toolBar, BorderLayout.NORTH);

		this.btnTiepNhanBenhNhan = new JButton(
				"Ti\u1EBFp nh\u1EADn b\u1EC7nh nh\u00E2n");
		this.btnTiepNhanBenhNhan.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DlgTiepNhanBenhNhan dlg = new DlgTiepNhanBenhNhan(null, true);
				dlg.show();
			}
		});
		this.btnTiepNhanBenhNhan
				.setIcon(new ImageIcon(
						MainApp.class
								.getResource("/com/pkdl/resources/mobi.az.uk.football.arsenalfc_icon.png")));
		this.btnTiepNhanBenhNhan.setVerticalTextPosition(SwingConstants.BOTTOM);
		this.btnTiepNhanBenhNhan
				.setHorizontalTextPosition(SwingConstants.CENTER);
		this.btnTiepNhanBenhNhan.setBorderPainted(false);
		toolBar.add(this.btnTiepNhanBenhNhan);

		this.btnBenhNhanHienThoi = new JButton(
				"B\u1EC7nh nh\u00E2n hi\u1EC7n th\u1EDDi");
		this.btnBenhNhanHienThoi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				DlgBenhNhanHienThoi dlg = new DlgBenhNhanHienThoi(null, true);
				dlg.show();
			}
		});
		this.btnBenhNhanHienThoi.setIcon(new ImageIcon(MainApp.class
				.getResource("/com/pkdl/resources/chelse10.png")));
		this.btnBenhNhanHienThoi.setVerticalTextPosition(SwingConstants.BOTTOM);
		this.btnBenhNhanHienThoi
				.setHorizontalTextPosition(SwingConstants.CENTER);
		this.btnBenhNhanHienThoi.setBorderPainted(false);
		toolBar.add(this.btnBenhNhanHienThoi);

		this.btnCapNhatBenhNhanHienThoi = new JButton(
				"C\u1EADp nh\u1EADt b\u1EC7nh nh\u00E2n hi\u1EC7n th\u1EDDi");
		this.btnCapNhatBenhNhanHienThoi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DlgCapNhatBenhNhanHienThoi dlg = new DlgCapNhatBenhNhanHienThoi(
						null, true);
				dlg.show();
			}
		});
		this.btnCapNhatBenhNhanHienThoi
				.setIcon(new ImageIcon(
						MainApp.class
								.getResource("/com/pkdl/resources/mobi.az.uk.football.arsenalfc_icon.png")));
		this.btnCapNhatBenhNhanHienThoi
				.setVerticalTextPosition(SwingConstants.BOTTOM);
		this.btnCapNhatBenhNhanHienThoi
				.setHorizontalTextPosition(SwingConstants.CENTER);
		this.btnCapNhatBenhNhanHienThoi.setBorderPainted(false);
		toolBar.add(this.btnCapNhatBenhNhanHienThoi);

		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setHorizontalTextPosition(SwingConstants.CENTER);
		lblNewLabel.setIcon(new ImageIcon(MainApp.class
				.getResource("/com/pkdl/resources/crm-flow.jpg")));
		panel.add(lblNewLabel, BorderLayout.CENTER);
	}

	private void showLoginForm() {
		DlgLogin dlg = new DlgLogin(null, true);
		dlg.show();

		AtomicReference<Object> errText = new AtomicReference<Object>("");
		if (!dlg.validateLogin(errText)) {
			JOptionPane.showMessageDialog(null, errText.get(), "Announcement",
					JOptionPane.ERROR_MESSAGE);
			this.showLoginForm();
		}
		return;
	}

}
